######### Notes ###########

# Demographics: All
#               Male, Female
#               16-24, 25-49, 50-64, 65+

# Other demographics not included
## Race (but non white categories very small n...)
## Class: Register General Social Class from 1983: 1=I; 2=II; 3=III.NM; 4=III.M; 5=IV; 6=V
## For over time analysis, NatCen advise Goldthorpe-Heath (5 category version) class – RGHClass – 1987 to 2000, and NS-SEC analytic class – RNSEG – thereafter (https://doc.ukdataservice.ac.uk/doc/8772/mrdoc/pdf/8772_bsa_2019_user_guide_v2.pdf)

# Could add RICHLAW - How much do you agree or disagree that: there is one law for the rich and one for the poor.



######### British Social Attitudes ###########

library(tidyverse)
library(haven) 

setwd("/Users/matteo/Desktop/Prison Polling/Prison Data Internship/Survey Data/Originals/BSAS/")

######### 1983 ###########

read_sav("BSAS 1983.sav") %>%
  mutate(rage = na_if(rage, -1)) %>% 
  mutate(rage = na_if(rage, 98)) %>% 
  mutate(rage = na_if(rage, 99)) %>% 
  mutate(rsex = na_if(rsex, 0)) %>% 
  mutate(rsex = na_if(rsex, 9)) %>% 
  mutate(rsex = case_match(rsex, 1 ~ "Male", 2 ~ "Female")) %>% 
  mutate(ethnicgp = case_match(ethnicgp, 1 ~ "asian", 2 ~ "black", 3 ~ "white", 4 ~ "other")) %>%
  select(rage, rsex, wtfactor, ethnicgp,
         spend1, spend2,
         victim, vmworry, safedark, burglary, vandal, mugging,
         pchelp, pcpolite, pcplease, pcsat,
         predict1, predict2, predict5, 
         cappun1, cappun2, cappun3, 
         obeylaw, breaklaw) %>%
  mutate(across(c(victim, safedark, burglary, vandal, mugging,
                  pchelp, pcpolite, pcplease, pcsat,
                  predict1, predict2, predict5, 
                  cappun1, cappun2, cappun3, 
                  obeylaw, breaklaw),
                ~if_else(. %in% c(-2,-1, 0, 9), NA_integer_, .))) %>%
  mutate(across(c(vmworry),
                ~if_else(. %in% c(-1, 9), NA_integer_, .))) %>%
  mutate(across(c(spend1, spend2),
                ~if_else(. %in% c(-2, -1, 0, 12, 99), NA_integer_, .))) %>%
  mutate(across(!(c(rage, rsex, wtfactor, ethnicgp)), ~tolower(as.character(as_factor(.))))) %>%
  mutate(Year = 1983) -> surveydata


######### 1984 ###########

read_sav("BSAS 1984.sav") %>%
  mutate(rage = na_if(rage, -1)) %>% 
  mutate(rage = na_if(rage, 98)) %>% 
  mutate(rage = na_if(rage, 99)) %>% 
  mutate(rsex = na_if(rsex, 0)) %>% 
  mutate(rsex = na_if(rsex, 9)) %>% 
  mutate(rsex = case_match(rsex, 1 ~ "Male", 2 ~ "Female")) %>% 
  mutate(ethnicgp = case_match(ethnicgp, 1 ~ "white", 2 ~ "asian", 3 ~ "black", 4 ~ "other")) %>%
  select(rage, rsex, wtfactor, ethnicgp,
         spend1, spend2,
         breaklaw,
         pchelp,
         predict1, predict2, predict5, 
         cappun1, cappun2, cappun3, obeylaw) %>%
  mutate(across(c(breaklaw,
                  pchelp,
                  predict1, predict2, predict5, 
                  cappun1, cappun2, cappun3, obeylaw),
                ~if_else(. %in% c(-2,-1, 0, 9), NA_integer_, .))) %>%
  mutate(across(c(spend1, spend2),
                ~if_else(. %in% c(-2, -1, 0, 12, 99), NA_integer_, .))) %>%
  mutate(across(!(c(rage, rsex, wtfactor, ethnicgp)), ~tolower(as.character(as_factor(.))))) %>%
  mutate(Year = 1984) %>%
  bind_rows(., surveydata) -> surveydata

######### 1985 ###########

read_sav("BSAS 1985.sav") %>%
  mutate(rage = na_if(rage, -1)) %>% 
  mutate(rage = na_if(rage, 98)) %>% 
  mutate(rage = na_if(rage, 99)) %>% 
  mutate(rsex = na_if(rsex, 0)) %>% 
  mutate(rsex = na_if(rsex, 9)) %>% 
  mutate(rsex = case_match(rsex, 1 ~ "Male", 2 ~ "Female")) %>% 
  mutate(ethnicgp = case_match(ethnicgp, 1 ~ "white", 2 ~ "asian", 3 ~ "black", 4 ~ "other")) %>%
  select(rage, rsex, wtfactor, ethnicgp,
         spend1, spend2,
         scobeylw, crimin1, crimin2, crimin3, crimin4,
         justice,
         cappun1, cappun2, cappun3) %>%
  rename(obeylaw = scobeylw) %>%
  mutate(across(c(obeylaw, crimin1, crimin2, crimin3, crimin4,
                  justice,
                  cappun1, cappun2, cappun3),
                ~if_else(. %in% c(-2,-1, 0, 9), NA_integer_, .))) %>%
  mutate(across(c(spend1, spend2),
                ~if_else(. %in% c(-2, -1, 0, 12, 99), NA_integer_, .))) %>%
  mutate(across(!(c(rage, rsex, wtfactor, ethnicgp)), ~tolower(as.character(as_factor(.))))) %>%
  mutate(Year = 1985) %>%
  bind_rows(., surveydata) -> surveydata


######### 1986 ###########

read_sav("BSAS 1986.sav") %>%
  mutate(rage = na_if(rage, -1)) %>% 
  mutate(rage = na_if(rage, 98)) %>% 
  mutate(rage = na_if(rage, 99)) %>% 
  mutate(rsex = na_if(rsex, 0)) %>% 
  mutate(rsex = na_if(rsex, 9)) %>% 
  mutate(rsex = case_match(rsex, 1 ~ "Male", 2 ~ "Female")) %>% 
  mutate(ethnicgp = case_match(ethnicgp, 1 ~ "white", 2 ~ "asian", 3 ~ "black", 4 ~ "other")) %>%
  select(rage, rsex, wtfactor, ethnicgp,
         spend1, spend2,
         obeylaw, wronglaw, deathapp, stifsent, 
         breaklaw, justice, policrun,
         predict1, predict2, predict5) %>%
  mutate(across(c(obeylaw, wronglaw, deathapp, stifsent, 
                  breaklaw, justice, policrun,
                  predict1, predict2, predict5),
                ~if_else(. %in% c(-2,-1, 0, 9), NA_integer_, .))) %>%
  mutate(across(c(spend1, spend2),
                ~if_else(. %in% c(-2, -1, 0, 12, 99), NA_integer_, .))) %>%
  mutate(across(!(c(rage, rsex, wtfactor, ethnicgp)), ~tolower(as.character(as_factor(.))))) %>%
  mutate(Year = 1986) %>%
  bind_rows(., surveydata) -> surveydata


######### 1987 ###########

read_sav("BSAS 1987.sav") %>%
  mutate(rage = na_if(rage, -1)) %>% 
  mutate(rage = na_if(rage, 98)) %>% 
  mutate(rage = na_if(rage, 99)) %>% 
  mutate(rsex = na_if(rsex, 0)) %>% 
  mutate(rsex = na_if(rsex, 9)) %>% 
  mutate(rsex = case_match(rsex, 1 ~ "Male", 2 ~ "Female")) %>% 
  mutate(ethnicgp = case_match(ethnicgp, 1 ~ "white", 2 ~ "asian", 3 ~ "black", 4 ~ "other")) %>%
  select(rage, rsex, wtfactor, ethnicgp,
         spend1, spend2,
         policrun,
         wronglaw, deathapp, stifsent, 
         predict1, predict2, predict5) %>%
  mutate(across(c(policrun,
                  wronglaw, deathapp, stifsent, 
                  predict1, predict2, predict5),
                ~if_else(. %in% c(-2,-1, 0, 9), NA_integer_, .))) %>%
  mutate(across(c(spend1, spend2),
                ~if_else(. %in% c(-2, -1, 0, 12, 99), NA_integer_, .))) %>%
  mutate(across(!(c(rage, rsex, wtfactor, ethnicgp)), ~tolower(as.character(as_factor(.))))) %>%
  mutate(Year = 1987) %>%
  bind_rows(., surveydata) -> surveydata


######### 1989 ###########

read_sav("BSAS 1989.sav") %>%
  mutate(rage = na_if(rage, -1)) %>% 
  mutate(rage = na_if(rage, 98)) %>% 
  mutate(rage = na_if(rage, 99)) %>% 
  mutate(rsex = na_if(rsex, 0)) %>% 
  mutate(rsex = na_if(rsex, 9)) %>% 
  mutate(rsex = case_match(rsex, 1 ~ "Male", 2 ~ "Female")) %>% 
  mutate(ethnicgp = case_match(ethnicgp, 1 ~ "white", 2 ~ "asian", 3 ~ "black", 4 ~ "other")) %>%
  select(rage, rsex, wtfactor, ethnicgp,
         spend1, spend2,
         wronglaw, deathapp, stifsent,
         predict1, predict2, predict5, 
         cappun1, cappun2, cappun3,
         obeylaw) %>%
  mutate(across(c(wronglaw, deathapp, stifsent,
                  predict1, predict2, predict5, 
                  cappun1, cappun2, cappun3,
                  obeylaw),
                ~if_else(. %in% c(-2,-1, 0, 9), NA_integer_, .))) %>%
  mutate(across(c(spend1, spend2),
                ~if_else(. %in% c(-2, -1, 0, 12, 99), NA_integer_, .))) %>%
  mutate(across(!(c(rage, rsex, wtfactor, ethnicgp)), ~tolower(as.character(as_factor(.))))) %>%
  mutate(Year = 1989) %>%
  bind_rows(., surveydata) -> surveydata


######### 1990 ###########

read_por("bsa90.por") %>%
  mutate(RAGE = na_if(RAGE, -1)) %>% 
  mutate(RAGE = na_if(RAGE, 98)) %>% 
  mutate(RAGE = na_if(RAGE, 99)) %>% 
  mutate(rage = RAGE) %>%
  mutate(RSEX = na_if(RSEX, 0)) %>% 
  mutate(RSEX = na_if(RSEX, 9)) %>% 
  mutate(rsex = case_match(RSEX, 1 ~ "Male", 2 ~ "Female")) %>% 
  mutate(ethnicgp = case_match(RACEORIG, 1 ~ 'black', c(2,3,4,6) ~ 'asian',
                               c(7,8,9) ~ 'white', 5 ~ 'other')) %>%
  mutate(wtfactor = WTFACTOR,
         spend1 = SPEND1, spend2 = SPEND2,
         obeylaw = SCOBEYLW, wronglaw = WRONGLAW, deathapp = DEATHAPP, stifsent = STIFSENT,
         victim = VICTIM, vmworry = VMWORRY,
         safedark = SAFEDARK, burglary = BURGLARY, vandal = VANDAL, mugging = MUGGING2,
         pchelp = PCHELP, pcpolite = PCPOLITE, pcplease = PCPLEASE, pcsat = PCSAT,
         graffiti = GRAFFITI, vandals = VANDALS, 
         crimin1 = CRIMIN1, crimin2 = CRIMIN2, crimin3 = CRIMIN3, crimin4 = CRIMIN4,
         justice = JUSTICE,
         prisbks = PRISBKS,
         pcnosolc = PCNOSOLC, idcards = IDCARDS, crimslnt = CRIMSLNT,
         predict1 = PREDICT1, predict2 = PREDICT2, predict5 = PREDICT5,
         cappun1 = CAPPUN1, cappun2 = CAPPUN2, cappun3 = CAPPUN3) %>%
  select(rage, rsex, wtfactor, ethnicgp,
         spend1, spend2,
         wronglaw, deathapp, stifsent, obeylaw,
         victim, vmworry,
         safedark, burglary, vandal, mugging,
         pchelp, pcpolite,pcplease,pcsat,
         graffiti, vandals,
         crimin1, crimin2, crimin3, crimin4, justice,
         prisbks, pcnosolc, idcards, crimslnt,
         predict1, predict2, predict5, 
         cappun1, cappun2, cappun3) %>%
  mutate(across(c(wronglaw, deathapp, stifsent, obeylaw,
                  victim,
                  safedark, burglary, vandal, mugging,
                  pchelp, pcpolite,pcplease,pcsat,
                  graffiti, vandals,
                  crimin1, crimin2, crimin3, crimin4, justice,
                  prisbks, pcnosolc, idcards, crimslnt,
                  predict1, predict2, predict5, 
                  cappun1, cappun2, cappun3),
                ~if_else(. %in% c(-2,-1, 0, 9), NA_integer_, .))) %>%
  mutate(across(c(vmworry),
                ~if_else(. %in% c(-1, 9), NA_integer_, .))) %>%
  mutate(across(c(spend1, spend2),
                ~if_else(. %in% c(-2, -1, 0, 12, 99), NA_integer_, .))) %>%
  mutate(across(!(c(rage, rsex, wtfactor, ethnicgp)), ~tolower(as.character(as_factor(.))))) %>%
  mutate(Year = 1990) %>%
  bind_rows(., surveydata) -> surveydata


######### 1991 ###########

read_sav("BSAS 1991.sav") %>%
  mutate(rage = na_if(rage, -1)) %>% 
  mutate(rage = na_if(rage, 98)) %>% 
  mutate(rage = na_if(rage, 99)) %>% 
  mutate(rsex = na_if(rsex, 0)) %>% 
  mutate(rsex = na_if(rsex, 9)) %>% 
  mutate(rsex = case_match(rsex, 1 ~ "Male", 2 ~ "Female")) %>% 
  mutate(ethnicgp = case_match(raceorig, 1 ~ 'black', c(2,3,4,6) ~ 'asian',
                               c(7,8,9) ~ 'white', 5 ~ 'other')) %>%
  select(rage, rsex, wtfactor, ethnicgp,
         spend1, spend2,
         breaklaw, prisnmor, prisnlss,
         wronglaw, deathapp, stifsent, stifrsnt,
         predict1, predict2, predict5,
         obeylaw) %>%
  mutate(across(c(breaklaw, prisnmor, prisnlss,
                  wronglaw, deathapp, stifsent, stifrsnt,
                  predict1, predict2, predict5,
                  obeylaw),
                ~if_else(. %in% c(-2,-1, 0, 9), NA_integer_, .))) %>%
  mutate(across(c(spend1, spend2),
                ~if_else(. %in% c(-2, -1, 0, 12, 99), NA_integer_, .))) %>%
  mutate(across(!(c(rage, rsex, wtfactor, ethnicgp)), ~tolower(as.character(as_factor(.))))) %>%
  mutate(Year = 1991) %>%
  bind_rows(., surveydata) -> surveydata


######### 1993 ###########

read_sav("BSAS 1993.sav") %>%
  mutate(rage = na_if(rage, -1)) %>% 
  mutate(rage = na_if(rage, 98)) %>% 
  mutate(rage = na_if(rage, 99)) %>% 
  mutate(rsex = na_if(rsex, 0)) %>% 
  mutate(rsex = na_if(rsex, 9)) %>% 
  mutate(rsex = case_match(rsex, 1 ~ "Male", 2 ~ "Female")) %>% 
  mutate(ethnicgp = case_match(raceorig, 1 ~ 'black', c(2,3,4,6) ~ 'asian',
                               c(7,8,9) ~ 'white', 5 ~ 'other')) %>%
  select(rage, rsex, wtfactor, ethnicgp,
         spend1, spend2,
         wronglaw, deathapp, stifsent, cappun1, cappun2, cappun3) %>%
  mutate(across(c(wronglaw, deathapp, stifsent, cappun1, cappun2, cappun3),
                ~if_else(. %in% c(-2,-1, 0, 9), NA_integer_, .))) %>%
  mutate(across(c(spend1, spend2),
                ~if_else(. %in% c(-2, -1, 0, 12, 99), NA_integer_, .))) %>%
  mutate(across(!(c(rage, rsex, wtfactor, ethnicgp)), ~tolower(as.character(as_factor(.))))) %>%
  mutate(Year = 1993) %>%
  bind_rows(., surveydata) -> surveydata


######### 1994 ###########

read_sav("BSAS 1994.sav") %>%
  mutate(rage = na_if(rage, -1)) %>% 
  mutate(rage = na_if(rage, 98)) %>% 
  mutate(rage = na_if(rage, 99)) %>% 
  mutate(rsex = na_if(rsex, 0)) %>% 
  mutate(rsex = na_if(rsex, 9)) %>% 
  mutate(rsex = case_match(rsex, 1 ~ "Male", 2 ~ "Female")) %>%
  mutate(ethnicgp = case_match(raceorig, 1 ~ 'black', c(2,3,4,6) ~ 'asian',
                               c(7,8,9) ~ 'white', c(5,10) ~ 'other')) %>%
  select(rage, rsex, wtfactor, ethnicgp,
         spend1, spend2,
         wronglaw, breaklaw, obeylaw,
         victim, vmworry, safedark,
         graffiti, vandals, burglary, attacks, vehtheft,
         justice, deathapp, stifsent, 
         crimin1, crimin2, crimin3, crimin4,
         prisbks, prisvist, priscong, prisjob,
         pcnosolc, idcards, crimslnt,
         prissen1, prissen2, prissen3, prissen4, prissen5, prissen6,
         policrn2,
         predict1, predict2, predict5, 
         cappun1, cappun2, cappun3) %>%
  rename(policrun = policrn2) %>%
  mutate(across(c(wronglaw, breaklaw, obeylaw,
                  victim, vmworry, safedark,
                  graffiti, vandals, burglary, attacks, vehtheft,
                  justice, deathapp, stifsent, 
                  crimin1, crimin2, crimin3, crimin4,
                  prisbks, prisvist, priscong, prisjob,
                  pcnosolc, idcards, crimslnt,
                  prissen1, prissen2, prissen3, prissen4, prissen5, prissen6,
                  policrun,
                  predict1, predict2, predict5, 
                  cappun1, cappun2, cappun3),
                ~if_else(. %in% c(-2,-1, 0, 9), NA_integer_, .))) %>%
  mutate(across(c(spend1, spend2),
                ~if_else(. %in% c(-2, -1, 0, 12, 99), NA_integer_, .))) %>%
  mutate(across(!(c(rage, rsex, wtfactor, ethnicgp)), ~tolower(as.character(as_factor(.))))) %>%
  mutate(Year = 1994) %>%
  bind_rows(., surveydata) -> surveydata


######### 1995 ###########

read_sav("BSAS 1995.sav") %>%
  mutate(rage = na_if(rage, -1)) %>% 
  mutate(rage = na_if(rage, 98)) %>% 
  mutate(rage = na_if(rage, 99)) %>% 
  mutate(rsex = na_if(rsex, 0)) %>% 
  mutate(rsex = na_if(rsex, 9)) %>% 
  mutate(rsex = case_match(rsex, 1 ~ "Male", 2 ~ "Female")) %>% 
  mutate(ethnicgp = case_match(raceorig, 1 ~ 'black', c(2,3,4,6) ~ 'asian',
                               c(7,8,9) ~ 'white', c(5,10) ~ 'other')) %>%
  select(rage, rsex, wtfactor, ethnicgp,
         spend1, spend2,
         victim, vmworry, pchelp, pcpolite,
         wronglaw, deathapp, stifsent,
         cappun1, cappun2, cappun3) %>%
  mutate(across(c(victim, pchelp, pcpolite,
                  wronglaw, deathapp, stifsent,
                  cappun1, cappun2, cappun3),
                ~if_else(. %in% c(-2,-1, 0, 9), NA_integer_, .))) %>%
  mutate(across(c(vmworry),
                ~if_else(. %in% c(-2, -1, 9), NA_integer_, .))) %>%
  mutate(across(c(spend1, spend2),
                ~if_else(. %in% c(-2, -1, 0, 12, 99), NA_integer_, .))) %>%
  mutate(across(!(c(rage, rsex, wtfactor, ethnicgp)), ~tolower(as.character(as_factor(.))))) %>%
  mutate(Year = 1995) %>%
  bind_rows(., surveydata) -> surveydata


######### 1996 ###########

read_sav("BSAS 1996.sav") %>%
  mutate(rage = na_if(rage, -1)) %>% 
  mutate(rage = na_if(rage, 98)) %>% 
  mutate(rage = na_if(rage, 99)) %>% 
  mutate(rsex = na_if(rsex, 0)) %>% 
  mutate(rsex = na_if(rsex, 9)) %>% 
  mutate(rsex = case_match(rsex, 1 ~ "Male", 2 ~ "Female")) %>% 
  mutate(ethnicgp = case_match(raceori2, c(1,2,3) ~ 'black', c(4,5,6,8) ~ 'asian',
                               c(9,10) ~ 'white', c(7,11,12) ~ 'other')) %>%
  select(rage, rsex, wtfactor, ethnicgp,
         spend1, spend2,
         wronglaw, obeylaw, deathapp, stifsent,
         stifrsnt, prisnmor, prisnlss,
         justice,
         prisbks, prisvist, priscong, prisjob, pcnosolc, idcards,
         prissen1, prissen2, prissen3, prissen4, prissen5, prissen6,
         graffiti, vandals, burglary, attacks, vehtheft) %>%
  mutate(across(c(wronglaw, obeylaw, deathapp, stifsent,
                  stifrsnt, prisnmor, prisnlss,
                  justice,
                  prisbks, prisvist, priscong, prisjob, pcnosolc, idcards,
                  prissen1, prissen2, prissen3, prissen4, prissen5, prissen6,
                  graffiti, vandals, burglary, attacks, vehtheft),
                ~if_else(. %in% c(-2,-1, 0, 9), NA_integer_, .))) %>%
  mutate(across(c(spend1, spend2),
                ~if_else(. %in% c(-2, -1, 0, 12, 99), NA_integer_, .))) %>%
  mutate(across(!(c(rage, rsex, wtfactor, ethnicgp)), ~tolower(as.character(as_factor(.))))) %>%
  mutate(Year = 1996) %>%
  bind_rows(., surveydata) -> surveydata


######### 1997 ###########

read_por("bsa1997.por") %>%
  mutate(RAGE = na_if(RAGE, -1)) %>% 
  mutate(RAGE = na_if(RAGE, 98)) %>% 
  mutate(rage = na_if(RAGE, 99)) %>% 
  mutate(RSEX = na_if(RSEX, 0)) %>% 
  mutate(RSEX = na_if(RSEX, 9)) %>% 
  mutate(rsex = case_match(RSEX, 1 ~ "Male", 2 ~ "Female")) %>% 
  mutate(wtfactor = WTFACTOR,
         spend1 = SPEND1, spend2 = SPEND2) %>%
  mutate(ethnicgp = case_match(RACEORI2, c(1,2,3) ~ 'black', c(4,5,6,8) ~ 'asian',
                               c(9,10) ~ 'white', c(7,11,12) ~ 'other')) %>%
  select(rage, rsex, wtfactor, ethnicgp,
         spend1, spend2) %>%
  mutate(across(c(spend1, spend2),
                ~if_else(. %in% c(-2, -1, 0, 12, 99), NA_integer_, .))) %>%
  mutate(across(!(c(rage, rsex, wtfactor, ethnicgp)), ~tolower(as.character(as_factor(.))))) %>%
  mutate(Year = 1997) %>%
  bind_rows(., surveydata) -> surveydata


######### 1998 ###########

read_sav("BSAS 1998.sav") %>%
  mutate(rage = na_if(rage, -1)) %>% 
  mutate(rage = na_if(rage, 98)) %>% 
  mutate(rage = na_if(rage, 99)) %>% 
  mutate(rsex = na_if(rsex, 0)) %>% 
  mutate(rsex = na_if(rsex, 9)) %>% 
  mutate(rsex = case_match(rsex, 1 ~ "Male", 2 ~ "Female")) %>% 
  mutate(ethnicgp = case_match(raceori2, c(1,2,3) ~ 'black', c(4,5,6,8) ~ 'asian',
                               c(9,10) ~ 'white', c(7,11,12) ~ 'other')) %>%
  select(rage, rsex, wtfactor, ethnicgp,
         graffiti, vandals, burglary, attacks, vehtheft, 
         deathapp, stifsent) %>%
  mutate(across(c(graffiti, vandals, burglary, attacks, vehtheft, deathapp, stifsent),
                ~if_else(. %in% c(-2,-1, 0, 9), NA_integer_, .))) %>%
  mutate(across(!(c(rage, rsex, wtfactor, ethnicgp)), ~tolower(as.character(as_factor(.))))) %>%
  mutate(Year = 1998) %>%
  bind_rows(., surveydata) -> surveydata


######### 1999 ###########

read_sav("BSAS 1999.sav") %>%
  mutate(rage = na_if(rage, -1)) %>% 
  mutate(rage = na_if(rage, 98)) %>% 
  mutate(rage = na_if(rage, 99)) %>% 
  mutate(rsex = na_if(rsex, 0)) %>% 
  mutate(rsex = na_if(rsex, 9)) %>% 
  mutate(rsex = case_match(rsex, 1 ~ "Male", 2 ~ "Female")) %>%
  mutate(ethnicgp = case_match(raceori2, c(1,2,3) ~ 'black', c(4,5,6,8) ~ 'asian',
                               c(9,10) ~ 'white', c(7,11,12) ~ 'other')) %>%
  select(rage, rsex, wtfactor, ethnicgp,
         spend1, spend2,
         stifsent, deathapp) %>%
  mutate(across(c(stifsent, deathapp),
                ~if_else(. %in% c(-2,-1, 0, 9), NA_integer_, .))) %>%
  mutate(across(c(spend1, spend2),
                ~if_else(. %in% c(-2, -1, 0, 12, 99), NA_integer_, .))) %>%
  mutate(across(!(c(rage, rsex, wtfactor, ethnicgp)), ~tolower(as.character(as_factor(.))))) %>%
  mutate(Year = 1999) %>%
  bind_rows(., surveydata) -> surveydata


######### 2000 ###########

read_sav("BSAS 2000.sav") %>%
  mutate(rage = na_if(rage, -1)) %>% 
  mutate(rage = na_if(rage, 98)) %>% 
  mutate(rage = na_if(rage, 99)) %>% 
  mutate(rsex = na_if(rsex, 0)) %>% 
  mutate(rsex = na_if(rsex, 9)) %>% 
  mutate(rsex = case_match(rsex, 1 ~ "Male", 2 ~ "Female")) %>%
  mutate(ethnicgp = case_match(raceori2, c(1,2,3) ~ 'black', c(4,5,6,8) ~ 'asian',
                               c(9,10) ~ 'white', c(7,11,12) ~ 'other')) %>%
  select(rage, rsex, wtfactor, ethnicgp,
         spend1, spend2,
         wronglaw, stifsent, deathapp) %>%
  mutate(across(c(wronglaw, stifsent, deathapp),
                ~if_else(. %in% c(-2,-1, 0, 9), NA_integer_, .))) %>%
  mutate(across(c(spend1, spend2),
                ~if_else(. %in% c(-2, -1, 0, 12, 99), NA_integer_, .))) %>%
  mutate(across(!(c(rage, rsex, wtfactor, ethnicgp)), ~tolower(as.character(as_factor(.))))) %>%
  mutate(Year = 2000) %>%
  bind_rows(., surveydata) -> surveydata


######### 2001 ###########

read_por("bsa01.por") %>%
  mutate(RAGE = na_if(RAGE, -1)) %>% 
  mutate(RAGE = na_if(RAGE, 98)) %>% 
  mutate(rage = na_if(RAGE, 99)) %>% 
  mutate(RSEX = na_if(RSEX, 0)) %>% 
  mutate(RSEX = na_if(RSEX, 9)) %>% 
  mutate(rsex = case_match(RSEX, 1 ~ "Male", 2 ~ "Female")) %>% 
  mutate(wtfactor = WTFACTOR,
         spend1 = SPEND1, spend2 = SPEND2,
         wronglaw = WRONGLAW, deathapp = DEATHAPP, stifsent = STIFSENT) %>%
  mutate(ethnicgp = case_match(RACEORI2, c(1,2,3) ~ 'black', c(4,5,6,8) ~ 'asian',
                               c(9,10) ~ 'white', c(7,11,12) ~ 'other')) %>%
  select(rage, rsex, wtfactor, ethnicgp,
         spend1, spend2,
         stifsent, deathapp, wronglaw) %>%
  mutate(across(c(stifsent, deathapp, wronglaw),
                ~if_else(. %in% c(-2,-1, 0, 9), NA_integer_, .))) %>%
  mutate(across(c(spend1, spend2),
                ~if_else(. %in% c(-2, -1, 0, 12, 99), NA_integer_, .))) %>%
  mutate(across(!(c(rage, rsex, wtfactor, ethnicgp)), ~tolower(as.character(as_factor(.))))) %>%
  mutate(Year = 2001) %>%
  bind_rows(., surveydata) -> surveydata


######### 2002 ###########

read_por("bsa02.por") %>%
  mutate(RAGE = na_if(RAGE, -1)) %>% 
  mutate(RAGE = na_if(RAGE, 98)) %>% 
  mutate(rage = na_if(RAGE, 99)) %>% 
  mutate(RSEX = na_if(RSEX, 0)) %>% 
  mutate(RSEX = na_if(RSEX, 9)) %>% 
  mutate(rsex = case_match(RSEX, 1 ~ "Male", 2 ~ "Female")) %>% 
  mutate(wtfactor = WTFACTOR,
         spend1 = SPEND1, spend2 = SPEND2,
         wronglaw = WRONGLAW, deathapp = DEATHAPP, stifsent = STIFSENT) %>%
  mutate(ethnicgp = case_match(RACEORI2, c(1,2,3) ~ 'black', c(4,5,6,8) ~ 'asian',
                               c(9,10) ~ 'white', c(7,11,12) ~ 'other')) %>%
  select(rage, rsex, wtfactor, ethnicgp,
         spend1, spend2,
         stifsent, deathapp, wronglaw) %>%
  mutate(across(c(stifsent, deathapp, wronglaw),
                ~if_else(. %in% c(-2,-1, 0, 9), NA_integer_, .))) %>%
  mutate(across(c(spend1, spend2),
                ~if_else(. %in% c(-2, -1, 0, 12, 99), NA_integer_, .))) %>%
  mutate(across(!(c(rage, rsex, wtfactor, ethnicgp)), ~tolower(as.character(as_factor(.))))) %>%
  mutate(Year = 2002) %>%
  bind_rows(., surveydata) -> surveydata


######### 2003 ###########

read_por("bsa03.por") %>%
  mutate(RAGE = na_if(RAGE, -1)) %>% 
  mutate(RAGE = na_if(RAGE, 98)) %>% 
  mutate(rage = na_if(RAGE, 99)) %>% 
  mutate(RSEX = na_if(RSEX, 0)) %>% 
  mutate(RSEX = na_if(RSEX, 9)) %>% 
  mutate(rsex = case_match(RSEX, 1 ~ "Male", 2 ~ "Female")) %>% 
  mutate(wtfactor = WTFACTOR,
         spend1 = SPEND1, spend2 = SPEND2,
         wronglaw = WRONGLAW, deathapp = DEATHAPP, stifsent = STIFSENT) %>%
  mutate(ethnicgp = case_match(RACEORI2, c(1,2,3) ~ 'black', c(4,5,6,8) ~ 'asian',
                               c(9,10) ~ 'white', c(7,11,12) ~ 'other')) %>%
  select(rage, rsex, wtfactor, ethnicgp,
         spend1, spend2,
         stifsent, deathapp, wronglaw) %>%
  mutate(across(c(stifsent, deathapp, wronglaw),
                ~if_else(. %in% c(-2,-1, 0, 9), NA_integer_, .))) %>%
  mutate(across(c(spend1, spend2),
                ~if_else(. %in% c(-2, -1, 0, 12, 99), NA_integer_, .))) %>%
  mutate(across(!(c(rage, rsex, wtfactor, ethnicgp)), ~tolower(as.character(as_factor(.))))) %>%
  mutate(Year = 2003) %>%
  bind_rows(., surveydata) -> surveydata


######### 2004 ###########

read_sav("BSAS 2004.sav") %>%
  mutate(RAge = na_if(RAge, -1)) %>% 
  mutate(RAge = na_if(RAge, 98)) %>% 
  mutate(rage = na_if(RAge, 99)) %>% 
  mutate(RSex = na_if(RSex, 0)) %>% 
  mutate(RSex = na_if(RSex, 9)) %>% 
  mutate(rsex = case_match(RSex, 1 ~ "Male", 2 ~ "Female")) %>%
  mutate(wtfactor = WtFactor,
         spend1 = Spend1, spend2 = Spend2,
         pcnosolc = PCNoSolc, idcards = IDCards,
         wronglaw = WrongLaw, deathapp = DeathApp, stifsent = StifSent) %>%
  mutate(ethnicgp = case_match(RaceOri2, c(1,2,3) ~ 'black', c(4,5,6,8) ~ 'asian',
                               c(9,10) ~ 'white', c(7,11,12) ~ 'other')) %>%
  select(rage, rsex, wtfactor, ethnicgp,
         spend1, spend2,
         pcnosolc, idcards,
         stifsent, deathapp, wronglaw) %>%
  mutate(across(c(pcnosolc, idcards,
                  stifsent, deathapp, wronglaw),
                ~if_else(. %in% c(-2,-1, 0, 9), NA_integer_, .))) %>%
  mutate(across(c(spend1, spend2),
                ~if_else(. %in% c(-2, -1, 0, 12, 99), NA_integer_, .))) %>%
  mutate(across(!(c(rage, rsex, wtfactor, ethnicgp)), ~tolower(as.character(as_factor(.))))) %>%
  mutate(Year = 2004) %>%
  bind_rows(., surveydata) -> surveydata


######### 2005 ###########

read_sav("BSAS 2005.sav") %>%
  mutate(RAge = na_if(RAge, -1)) %>% 
  mutate(RAge = na_if(RAge, 98)) %>% 
  mutate(rage = na_if(RAge, 99)) %>% 
  mutate(RSex = na_if(RSex, 0)) %>% 
  mutate(RSex = na_if(RSex, 9)) %>% 
  mutate(rsex = case_match(RSex, 1 ~ "Male", 2 ~ "Female")) %>%
  mutate(wtfactor = WtFactor,
         spend1 = Spend1, spend2 = Spend2,
         pcnosolc = PCNoSolc, idcards = IDCards, justice = Justice,
         wronglaw = WrongLaw, deathapp = DeathApp, stifsent = StifSent, 
         obeylaw = ObeyLaw) %>%
  mutate(ethnicgp = case_match(RaceOri2, c(1,2,3) ~ 'black', c(4,5,6,8) ~ 'asian',
                               c(9,10) ~ 'white', c(7,11,12) ~ 'other')) %>%
  select(rage, rsex, wtfactor, ethnicgp,
         spend1, spend2,
         pcnosolc, idcards, justice,
         stifsent, deathapp, wronglaw, obeylaw) %>%
  mutate(across(c(pcnosolc, idcards, justice,
                  stifsent, deathapp, wronglaw, obeylaw),
                ~if_else(. %in% c(-2,-1, 0, 9), NA_integer_, .))) %>%
  mutate(across(c(spend1, spend2),
                ~if_else(. %in% c(-2, -1, 0, 12, 99), NA_integer_, .))) %>%
  mutate(across(!(c(rage, rsex, wtfactor, ethnicgp)), ~tolower(as.character(as_factor(.))))) %>%
  mutate(Year = 2005) %>%
  bind_rows(., surveydata) -> surveydata


######### 2006 ###########

read_sav("BSAS 2006.sav") %>%
  mutate(RAge = na_if(RAge, -1)) %>% 
  mutate(RAge = na_if(RAge, 98)) %>% 
  mutate(rage = na_if(RAge, 99)) %>% 
  mutate(RSex = na_if(RSex, 0)) %>% 
  mutate(RSex = na_if(RSex, 9)) %>% 
  mutate(rsex = case_match(RSex, 1 ~ "Male", 2 ~ "Female")) %>%
  mutate(wtfactor = WtFactor,
         spend1 = Spend1, spend2 = Spend2,
         graffiti = Graffiti, vandals = Vandals,
         obeylaw = SCObeyLw,
         pcnosolc = PCNoSolc, idcards = IDCards, justice = Justice,
         wronglaw = WrongLaw, deathapp = DeathApp, stifsent = StifSent) %>%
  mutate(ethnicgp = case_match(RaceOri2, c(1,2,3) ~ 'black', c(4,5,6,8) ~ 'asian',
                               c(9,10) ~ 'white', c(7,11,12) ~ 'other')) %>%
  select(rage, rsex, wtfactor, ethnicgp,
         spend1, spend2,
         graffiti, vandals, obeylaw, 
         pcnosolc, idcards, justice,
         stifsent, deathapp, wronglaw) %>%
  mutate(across(c(graffiti, vandals, obeylaw, 
                  pcnosolc, idcards, justice,
                  stifsent, deathapp, wronglaw),
                ~if_else(. %in% c(-2,-1, 0, 9), NA_integer_, .))) %>%
  mutate(across(c(spend1, spend2),
                ~if_else(. %in% c(-2, -1, 0, 12, 99), NA_integer_, .))) %>%
  mutate(across(!(c(rage, rsex, wtfactor, ethnicgp)), ~tolower(as.character(as_factor(.))))) %>%
  mutate(Year = 2006) %>%
  bind_rows(., surveydata) -> surveydata


######### 2007 ###########

read_sav("BSAS 2007.sav") %>%
  mutate(RAge = na_if(RAge, -1)) %>% 
  mutate(RAge = na_if(RAge, 98)) %>% 
  mutate(rage = na_if(RAge, 99)) %>% 
  mutate(RSex = na_if(RSex, 0)) %>% 
  mutate(RSex = na_if(RSex, 9)) %>% 
  mutate(rsex = case_match(RSex, 1 ~ "Male", 2 ~ "Female")) %>%
  mutate(wtfactor = WtFactor,
         spend1 = Spend1, spend2 = Spend2,
         graffiti = Graffiti, vandals = Vandals,
         pcnosolc = PCNoSolc, idcards = IDCards,
         wronglaw = WrongLaw, deathapp = DeathApp, stifsent = StifSent) %>%
  mutate(ethnicgp = case_match(RaceOri2, c(1,2,3) ~ 'black', c(4,5,6,8) ~ 'asian',
                               c(9,10) ~ 'white', c(7,11,12) ~ 'other')) %>%
  select(rage, rsex, wtfactor, ethnicgp,
         spend1, spend2,
         graffiti, vandals, 
         pcnosolc, idcards,
         stifsent, deathapp, wronglaw) %>%
  mutate(across(c(graffiti, vandals, 
                  pcnosolc, idcards,
                  stifsent, deathapp, wronglaw),
                ~if_else(. %in% c(-2,-1, 0, 9), NA_integer_, .))) %>%
  mutate(across(c(spend1, spend2),
                ~if_else(. %in% c(-2, -1, 0, 12, 99), NA_integer_, .))) %>%
  mutate(across(!(c(rage, rsex, wtfactor, ethnicgp)), ~tolower(as.character(as_factor(.))))) %>%
  mutate(Year = 2007) %>%
  bind_rows(., surveydata) -> surveydata


######### 2008 ###########

read_sav("BSAS 2008.sav") %>%
  mutate(RAge = na_if(RAge, -1)) %>% 
  mutate(RAge = na_if(RAge, 98)) %>% 
  mutate(rage = na_if(RAge, 99)) %>% 
  mutate(RSex = na_if(RSex, 0)) %>% 
  mutate(RSex = na_if(RSex, 9)) %>% 
  mutate(rsex = case_match(RSex, 1 ~ "Male", 2 ~ "Female")) %>%
  mutate(wtfactor = WtFactor,
         spend1 = Spend1, spend2 = Spend2,
         Safe10yr = Safe10yr,
         pcnosolc = PCNoSolc, idcards = IDCards,
         wronglaw = WrongLaw, deathapp = DeathApp, stifsent = StifSent) %>%
  mutate(ethnicgp = case_match(RaceOri2, c(1,2,3) ~ 'black', c(4,5,6,8) ~ 'asian',
                               c(9,10) ~ 'white', c(7,11,12) ~ 'other')) %>%
  select(rage, rsex, wtfactor, ethnicgp,
         spend1, spend2,
         pcnosolc, idcards, Safe10yr,
         stifsent, deathapp, wronglaw) %>%
  mutate(across(c(pcnosolc, idcards, Safe10yr,
                  stifsent, deathapp, wronglaw),
                ~if_else(. %in% c(-2,-1, 0, 9), NA_integer_, .))) %>%
  mutate(across(c(spend1, spend2),
                ~if_else(. %in% c(-2, -1, 0, 12, 99), NA_integer_, .))) %>%
  mutate(across(!(c(rage, rsex, wtfactor, ethnicgp)), ~tolower(as.character(as_factor(.))))) %>%
  mutate(Year = 2008) %>%
  bind_rows(., surveydata) -> surveydata


######### 2009 ###########

read_sav("BSAS 2009.sav") %>%
  mutate(RAge = na_if(RAge, -1)) %>% 
  mutate(RAge = na_if(RAge, 98)) %>% 
  mutate(rage = na_if(RAge, 99)) %>% 
  mutate(RSex = na_if(RSex, 0)) %>% 
  mutate(RSex = na_if(RSex, 9)) %>% 
  mutate(rsex = case_match(RSex, 1 ~ "Male", 2 ~ "Female")) %>%
  mutate(wtfactor = WtFactor,
         spend1 = Spend1, spend2 = Spend2,
         policrun = PolicRn2,
         wronglaw = WrongLaw, deathapp = DeathApp, stifsent = StifSent) %>%
  mutate(ethnicgp = case_match(RaceOri2, c(1,2,3) ~ 'black', c(4,5,6,8) ~ 'asian',
                               c(9,10) ~ 'white', c(7,11,12) ~ 'other')) %>%
  select(rage, rsex, wtfactor, ethnicgp,
         spend1, spend2,
         policrun, 
         stifsent, deathapp, wronglaw) %>%
  mutate(across(c(policrun, stifsent, deathapp, wronglaw),
                ~if_else(. %in% c(-2,-1, 0, 9), NA_integer_, .))) %>%
  mutate(across(c(spend1, spend2),
                ~if_else(. %in% c(-2, -1, 0, 12, 99), NA_integer_, .))) %>%
  mutate(across(!(c(rage, rsex, wtfactor, ethnicgp)), ~tolower(as.character(as_factor(.))))) %>%
  mutate(Year = 2009) %>%
  bind_rows(., surveydata) -> surveydata


######### 2010 ###########

read_sav("BSAS 2010.sav") %>%
  mutate(RAge = na_if(RAge, -1)) %>% 
  mutate(RAge = na_if(RAge, 98)) %>% 
  mutate(rage = na_if(RAge, 99)) %>% 
  mutate(RSex = na_if(RSex, 0)) %>% 
  mutate(RSex = na_if(RSex, 9)) %>% 
  mutate(rsex = case_match(RSex, 1 ~ "Male", 2 ~ "Female")) %>%
  mutate(wtfactor = WtFactor,
         spend1 = Spend1, spend2 = Spend2,
         wronglaw = WrongLaw, deathapp = DeathApp, stifsent = StifSent) %>%
  mutate(ethnicgp = case_match(RaceOri3, c(1,2,3) ~ 'black', c(4,5,6,8) ~ 'asian',
                               c(9) ~ 'white', c(7,10,11) ~ 'other')) %>%
  select(rage, rsex, wtfactor, ethnicgp,
         spend1, spend2,
         Safe10yr,
         stifsent, deathapp, wronglaw) %>%
  mutate(across(c(Safe10yr, stifsent, deathapp, wronglaw),
                ~if_else(. %in% c(-2,-1, 0, 9), NA_integer_, .))) %>%
  mutate(across(c(spend1, spend2),
                ~if_else(. %in% c(-2, -1, 0, 12, 99), NA_integer_, .))) %>%
  mutate(across(!(c(rage, rsex, wtfactor, ethnicgp)), ~tolower(as.character(as_factor(.))))) %>%
  mutate(Year = 2010) %>%
  bind_rows(., surveydata) -> surveydata


######### 2011 ###########

read_sav("BSAS 2011.sav") %>%
  mutate(RAge = na_if(RAge, -1)) %>% 
  mutate(RAge = na_if(RAge, 98)) %>% 
  mutate(rage = na_if(RAge, 99)) %>% 
  mutate(RSex = na_if(RSex, 0)) %>% 
  mutate(RSex = na_if(RSex, 9)) %>% 
  mutate(rsex = case_match(RSex, 1 ~ "Male", 2 ~ "Female")) %>%
  mutate(wtfactor = WtFactor,
         spend1 = Spend1, spend2 = Spend2,
         wronglaw = WrongLaw, deathapp = DeathApp, stifsent = StifSent) %>%
  mutate(ethnicgp = case_match(RaceOri3, c(1,2,3) ~ 'black', c(4,5,6,8) ~ 'asian',
                               c(9) ~ 'white', c(7,10,11) ~ 'other')) %>%
  select(rage, rsex, wtfactor, ethnicgp,
         spend1, spend2,
         stifsent, deathapp, wronglaw) %>%
  mutate(across(c(stifsent, deathapp, wronglaw),
                ~if_else(. %in% c(-2,-1, 0, 9), NA_integer_, .))) %>%
  mutate(across(c(spend1, spend2),
                ~if_else(. %in% c(-2, -1, 0, 12, 99), NA_integer_, .))) %>%
  mutate(across(!(c(rage, rsex, wtfactor, ethnicgp)), ~tolower(as.character(as_factor(.))))) %>%
  mutate(Year = 2011) %>%
  bind_rows(., surveydata) -> surveydata



######### 2012 ###########

read_sav("BSAS 2012.sav") %>%
  mutate(RAge = na_if(RAge, -1)) %>% 
  mutate(RAge = na_if(RAge, 98)) %>% 
  mutate(rage = na_if(RAge, 99)) %>% 
  mutate(RSex = na_if(RSex, 0)) %>% 
  mutate(RSex = na_if(RSex, 9)) %>% 
  mutate(rsex = case_match(RSex, 1 ~ "Male", 2 ~ "Female")) %>%
  mutate(wtfactor = WtFactor,
         spend1 = Spend1, spend2 = Spend2,
         policrun = policrn2, 
         wronglaw = WrongLaw, deathapp = DeathApp, stifsent = StifSent) %>%
  mutate(ethnicgp = case_match(RaceOri3, c(1,2,3) ~ 'black', c(4,5,6,8) ~ 'asian',
                               c(9) ~ 'white', c(7,10,11) ~ 'other')) %>%
  select(rage, rsex, wtfactor, ethnicgp,
         spend1, spend2,
         stifsent, deathapp, wronglaw, policrun,
         predict1, predict2, predict5) %>%
  mutate(across(c(stifsent, deathapp, wronglaw, policrun,
                  predict1, predict2, predict5),
                ~if_else(. %in% c(-2,-1, 0, 9), NA_integer_, .))) %>%
  mutate(across(c(spend1, spend2),
                ~if_else(. %in% c(-2, -1, 0, 12, 99), NA_integer_, .))) %>%
  mutate(across(!(c(rage, rsex, wtfactor, ethnicgp)), ~tolower(as.character(as_factor(.))))) %>%
  mutate(Year = 2012) %>%
  bind_rows(., surveydata) -> surveydata


######### 2013 ###########

read_sav("BSAS 2013.sav") %>%
  mutate(RAge = na_if(RAge, -1)) %>% 
  mutate(RAge = na_if(RAge, 98)) %>% 
  mutate(rage = na_if(RAge, 99)) %>% 
  mutate(RSex = na_if(RSex, 0)) %>% 
  mutate(RSex = na_if(RSex, 9)) %>% 
  mutate(rsex = case_match(RSex, 1 ~ "Male", 2 ~ "Female")) %>%
  mutate(wtfactor = WtFactor,
         spend1 = Spend1, spend2 = Spend2,
         wronglaw = WrongLaw, deathapp = DeathApp, stifsent = StifSent) %>%
  mutate(ethnicgp = case_match(RaceOri3, c(1,2,3) ~ 'black', c(4,5,6,8) ~ 'asian',
                               c(9) ~ 'white', c(7,10,11) ~ 'other')) %>%
  select(rage, rsex, wtfactor, ethnicgp,
         spend1, spend2,
         stifsent, deathapp, wronglaw) %>%
  mutate(across(c(stifsent, deathapp, wronglaw),
                ~if_else(. %in% c(-2,-1, 0, 9), NA_integer_, .))) %>%
  mutate(across(c(spend1, spend2),
                ~if_else(. %in% c(-2, -1, 0, 12, 99), NA_integer_, .))) %>%
  mutate(across(!(c(rage, rsex, wtfactor, ethnicgp)), ~tolower(as.character(as_factor(.))))) %>%
  mutate(Year = 2013) %>%
  bind_rows(., surveydata) -> surveydata



######### 2014 ###########

read_sav("BSAS 2014.sav") %>%
  mutate(Rage = na_if(Rage, -1)) %>% 
  mutate(Rage = na_if(Rage, 98)) %>% 
  mutate(rage = na_if(Rage, 99)) %>% 
  mutate(Rsex = na_if(Rsex, 0)) %>% 
  mutate(Rsex = na_if(Rsex, 9)) %>% 
  mutate(rsex = case_match(Rsex, 1 ~ "Male", 2 ~ "Female")) %>%
  mutate(wtfactor = WtFactor,
         spend1 = Spend1, spend2 = Spend2,
         policrun = policrn2) %>%
  mutate(ethnicgp = case_match(RaceOri3, c(1,2,3) ~ 'black', c(4,5,6,8) ~ 'asian',
                               c(9) ~ 'white', c(7,10,11) ~ 'other')) %>%
  select(rage, rsex, wtfactor, ethnicgp,
         spend1, spend2,
         Trstct, Trstpol, policrun,
         stifsent, deathapp, wronglaw) %>%
  mutate(across(c(Trstct, Trstpol, policrun,
                  stifsent, deathapp, wronglaw),
                ~if_else(. %in% c(-2,-1, 0, 9), NA_integer_, .))) %>%
  mutate(across(c(spend1, spend2),
                ~if_else(. %in% c(-2, -1, 0, 12, 99), NA_integer_, .))) %>%
  mutate(across(!(c(rage, rsex, wtfactor, ethnicgp)), ~tolower(as.character(as_factor(.))))) %>%
  mutate(Year = 2014) %>%
  bind_rows(., surveydata) -> surveydata



######### 2015 ###########

read_sav("BSAS 2015.sav") %>%
  mutate(Rage = na_if(Rage, -1)) %>% 
  mutate(Rage = na_if(Rage, 98)) %>% 
  mutate(rage = na_if(Rage, 99)) %>% 
  mutate(Rsex = na_if(Rsex, 0)) %>% 
  mutate(Rsex = na_if(Rsex, 9)) %>% 
  mutate(rsex = case_match(Rsex, 1 ~ "Male", 2 ~ "Female")) %>%
  mutate(wtfactor = WtFactor,
         spend1 = Spend1, spend2 = Spend2) %>%
  mutate(ethnicgp = case_match(RaceOri3, c(1,2,3) ~ 'black', c(4,5,6,8) ~ 'asian',
                               c(9) ~ 'white', c(7,10,11) ~ 'other')) %>%
  select(rage, rsex, wtfactor, ethnicgp,
         spend1, spend2,
         stifsent, deathapp, wronglaw) %>%
  mutate(across(c(stifsent, deathapp, wronglaw),
                ~if_else(. %in% c(-2,-1, 0, 9), NA_integer_, .))) %>%
  mutate(across(c(spend1, spend2),
                ~if_else(. %in% c(-2, -1, 0, 12, 99), NA_integer_, .))) %>%
  mutate(across(!(c(rage, rsex, wtfactor, ethnicgp)), ~tolower(as.character(as_factor(.))))) %>%
  mutate(Year = 2015) %>%
  bind_rows(., surveydata) -> surveydata


######### 2016 ###########

read_sav("BSAS 2016.sav") %>%
  mutate(Rage = na_if(Rage, -1)) %>% 
  mutate(Rage = na_if(Rage, 98)) %>% 
  mutate(rage = na_if(Rage, 99)) %>% 
  mutate(Rsex = na_if(Rsex, 0)) %>% 
  mutate(Rsex = na_if(Rsex, 9)) %>% 
  mutate(rsex = case_match(Rsex, 1 ~ "Male", 2 ~ "Female")) %>%
  mutate(wtfactor = WtFactor,
         spend1 = Spend1, spend2 = Spend2) %>%
  rename(obeylaw = scobeylw) %>%
  mutate(ethnicgp = case_match(RaceOri3, c(1,2,3) ~ 'black', c(4,5,6,8) ~ 'asian',
                               c(9) ~ 'white', c(7,10,11) ~ 'other')) %>%
  select(rage, rsex, wtfactor, ethnicgp,
         spend1, spend2,
         obeylaw, justice,
         stifsent, deathapp, wronglaw, Trstpol, Trstct) %>% 
  mutate(across(c(obeylaw, justice,
                  stifsent, deathapp, wronglaw, Trstpol, Trstct),
                ~if_else(. %in% c(-2,-1, 0, 9), NA_integer_, .))) %>%
  mutate(across(c(spend1, spend2),
                ~if_else(. %in% c(-2, -1, 0, 12, 99), NA_integer_, .))) %>%
  mutate(across(!(c(rage, rsex, wtfactor, ethnicgp)), ~tolower(as.character(as_factor(.))))) %>%
  mutate(Year = 2016) %>%
  bind_rows(., surveydata) -> surveydata




######### 2017 ###########

read_sav("BSAS 2017.sav") %>%
  mutate(RAgeE = na_if(RAgeE, -1)) %>% 
  mutate(RAgeE = na_if(RAgeE, 98)) %>% 
  mutate(rage = na_if(RAgeE, 99)) %>% 
  mutate(Rsex = na_if(Rsex, 0)) %>% 
  mutate(Rsex = na_if(Rsex, 9)) %>% 
  mutate(rsex = case_match(Rsex, 1 ~ "Male", 2 ~ "Female")) %>%
  mutate(wtfactor = WtFactor,
         spend1 = Spend1, spend2 = Spend2) %>%
  mutate(ethnicgp = case_match(raceori4, 1 ~ 'black', 2 ~ 'asian',
                               3 ~ 'white', c(4,5) ~ 'other')) %>%
  select(rage, rsex, wtfactor, ethnicgp,
         spend1, spend2,
         stifsent, deathapp, wronglaw) %>% 
  mutate(across(c(stifsent, deathapp, wronglaw),
                ~if_else(. %in% c(-2,-1, 0, 9), NA_integer_, .))) %>%
  mutate(across(c(spend1, spend2),
                ~if_else(. %in% c(-2, -1, 0, 12, 99), NA_integer_, .))) %>%
  mutate(across(!(c(rage, rsex, wtfactor, ethnicgp)), ~tolower(as.character(as_factor(.))))) %>%
  mutate(Year = 2017) %>%
  bind_rows(., surveydata) -> surveydata


######### 2018 ###########

read_sav("BSAS 2018.sav") %>%
  mutate(ragee = na_if(ragee, -1)) %>% 
  mutate(ragee = na_if(ragee, 98)) %>% 
  mutate(rage = na_if(ragee, 99)) %>% 
  mutate(RSex = na_if(RSex, 0)) %>% 
  mutate(RSex = na_if(RSex, 9)) %>% 
  mutate(rsex = case_match(RSex, 1 ~ "Male", 2 ~ "Female")) %>%
  mutate(wtfactor = WtFactor,
         spend1 = Spend1, spend2 = Spend2) %>%
  mutate(ethnicgp = case_match(raceori4, 1 ~ 'black', 2 ~ 'asian',
                               3 ~ 'white', c(4,5) ~ 'other')) %>%
  select(rage, rsex, wtfactor, ethnicgp,
         spend1, spend2,
         stifsent, deathapp, wronglaw, Trstpol, Trstct) %>% 
  mutate(across(c(stifsent, deathapp, wronglaw, Trstpol, Trstct),
                ~if_else(. %in% c(-2,-1, 0, 9), NA_integer_, .))) %>%
  mutate(across(c(spend1, spend2),
                ~if_else(. %in% c(-2, -1, 0, 12, 99), NA_integer_, .))) %>%
  mutate(across(!(c(rage, rsex, wtfactor, ethnicgp)), ~tolower(as.character(as_factor(.))))) %>%
  mutate(Year = 2018) %>%
  bind_rows(., surveydata) -> surveydata


######### 2019 ###########

read_sav("BSAS 2019.sav") %>%
  mutate(RageE  = na_if(RageE , -1)) %>% 
  mutate(RageE  = na_if(RageE , 98)) %>% 
  mutate(rage  = na_if(RageE , 99)) %>% 
  mutate(rsex = case_match(RSex, 1 ~ "Male", 2 ~ "Female")) %>%
  mutate(wtfactor = WtFactor,
         spend1 = Spend1, spend2 = Spend2) %>%
  mutate(ethnicgp = case_match(raceori4, 1 ~ 'black', 2 ~ 'asian',
                               3 ~ 'white', c(4,5) ~ 'other')) %>%
  select(rage, rsex, wtfactor, ethnicgp,
         spend1, spend2,
         stifsent, deathapp, wronglaw) %>% 
  mutate(across(c(stifsent, deathapp, wronglaw),
                ~if_else(. %in% c(-2,-1, 0, 9), NA_integer_, .))) %>%
  mutate(across(c(spend1, spend2),
                ~if_else(. %in% c(-2, -1, 0, 12, 99), NA_integer_, .))) %>%
  mutate(across(!(c(rage, rsex, wtfactor, ethnicgp)), ~tolower(as.character(as_factor(.))))) %>%
  mutate(Year = 2019) %>%
  bind_rows(., surveydata) -> surveydata


######### 2020 ###########

read_sav("BSAS 2020.sav") %>%
  mutate(RespAgeE = na_if(RespAgeE , -1)) %>% 
  mutate(RespAgeE  = na_if(RespAgeE , 98)) %>% 
  mutate(rage  = na_if(RespAgeE , 99)) %>% 
  mutate(rsex = case_match(RespSx2cat, 1 ~ "Female", 2 ~ "Male")) %>%
  mutate(wtfactor = BSA20_wt_new,
         spend1 = Spend1, spend2 = Spend2,
         wronglaw = WrongLaw, deathapp = DeathApp, stifsent = StifSent) %>%
  mutate(ethnicgp = case_match(RaceOri4, 1 ~ 'black', 2 ~ 'asian',
                               3 ~ 'white', c(4,5) ~ 'other')) %>%
  select(rage, rsex, wtfactor, ethnicgp,
         spend1, spend2,
         stifsent, deathapp, wronglaw) %>% 
  mutate(across(c(stifsent, deathapp, wronglaw),
                ~if_else(. %in% c(-2,-1, 0, 9), NA_integer_, .))) %>%
  mutate(across(c(spend1, spend2),
                ~if_else(. %in% c(-2, -1, 0, 12, 99), NA_integer_, .))) %>%
  mutate(across(!(c(rage, rsex, wtfactor, ethnicgp)), ~tolower(as.character(as_factor(.))))) %>%
  mutate(Year = 2020) %>%
  bind_rows(., surveydata) -> surveydata


######### 2021 ###########

read_sav("BSAS 2021.sav") %>%
  mutate(RespAge_Archive = na_if(RespAge_Archive , -1)) %>% 
  mutate(RespAge_Archive = na_if(RespAge_Archive , 98)) %>% 
  mutate(rage = na_if(RespAge_Archive , 99)) %>% 
  mutate(rsex = case_match(DVSex21, 1 ~ "Female", 2 ~ "Male")) %>%
  mutate(wtfactor = BSA21_final_wt,
         spend1 = Spend1, spend2 = Spend2,
         wronglaw = WrongLaw, deathapp = DeathApp, stifsent = StifSent,
         Trstpol = TrstPol) %>%
  mutate(ethnicgp = case_match(RaceOri4, 1 ~ 'black', 2 ~ 'asian',
                               3 ~ 'white', c(4,5) ~ 'other')) %>%
  select(rage, rsex, wtfactor, ethnicgp,
         spend1, spend2,
         stifsent, deathapp, wronglaw, Trstpol) %>% 
  mutate(across(c(stifsent, deathapp, wronglaw, Trstpol),
                ~if_else(. %in% c(-2,-1, 0, 9), NA_integer_, .))) %>%
  mutate(across(c(spend1, spend2),
                ~if_else(. %in% c(-2, -1, 0, 12, 99), NA_integer_, .))) %>%
  mutate(across(!(c(rage, rsex, wtfactor)), ~tolower(as.character(as_factor(.))))) %>%
  mutate(Year = 2021) %>%
  bind_rows(., surveydata) -> surveydata

write_csv(surveydata, "/Users/matteo/Desktop/Prison Polling/Prison Data Internship/Survey Data/Finished/BSA_allrepeatedcrimequestions.csv")


# Some of the weights are really high...